using Senparc.Weixin.MP.Helpers;
using System;
using System.Collections;
using System.Text;
using System.Xml;
namespace Senparc.Weixin.MP.TenPayLib
{
	public class ClientResponseHandler
	{
		private string Key;
		protected Hashtable Parameters;
		private string DebugInfo;
		protected string Content;
		private string Charset = "gb2312";
		public ClientResponseHandler()
		{
			this.Parameters = new Hashtable();
		}
		public string GetContent()
		{
			return this.Content;
		}
		public virtual void SetContent(string content)
		{
			this.Content = content;
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(content);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("root");
			XmlNodeList childNodes = xmlNode.ChildNodes;
			foreach (XmlNode xmlNode2 in childNodes)
			{
				this.SetParameter(xmlNode2.Name, xmlNode2.InnerXml);
			}
		}
		public string GetKey()
		{
			return this.Key;
		}
		public void SetKey(string key)
		{
			this.Key = key;
		}
		public string GetParameter(string parameter)
		{
			string text = (string)this.Parameters[parameter];
			return (text == null) ? "" : text;
		}
		public void SetParameter(string parameter, string parameterValue)
		{
			if (parameter != null && parameter != "")
			{
				if (this.Parameters.Contains(parameter))
				{
					this.Parameters.Remove(parameter);
				}
				this.Parameters.Add(parameter, parameterValue);
			}
		}
		public virtual bool IsTenpaySign()
		{
			StringBuilder stringBuilder = new StringBuilder();
			ArrayList arrayList = new ArrayList(this.Parameters.Keys);
			arrayList.Sort();
			foreach (string text in arrayList)
			{
				string text2 = (string)this.Parameters[text];
				if (text2 != null && "".CompareTo(text2) != 0 && "sign".CompareTo(text) != 0 && "key".CompareTo(text) != 0)
				{
					stringBuilder.Append(text + "=" + text2 + "&");
				}
			}
			stringBuilder.Append("key=" + this.GetKey());
			string text3 = MD5UtilHelper.GetMD5(stringBuilder.ToString(), this.getCharset()).ToLower();
			this.SetDebugInfo(stringBuilder.ToString() + " => sign:" + text3);
			return this.GetParameter("sign").ToLower().Equals(text3);
		}
		public string GetDebugInfo()
		{
			return this.DebugInfo;
		}
		protected void SetDebugInfo(string debugInfo)
		{
			this.DebugInfo = debugInfo;
		}
		protected virtual string getCharset()
		{
			return this.Charset;
		}
		public void SetCharset(string charset)
		{
			this.Charset = charset;
		}
		public virtual bool IsTenpaySign(ArrayList aKeys)
		{
			StringBuilder stringBuilder = new StringBuilder();
			foreach (string text in aKeys)
			{
				string text2 = (string)this.Parameters[text];
				if (text2 != null && "".CompareTo(text2) != 0 && "sign".CompareTo(text) != 0 && "key".CompareTo(text) != 0)
				{
					stringBuilder.Append(text + "=" + text2 + "&");
				}
			}
			stringBuilder.Append("key=" + this.GetKey());
			string text3 = MD5UtilHelper.GetMD5(stringBuilder.ToString(), this.getCharset()).ToLower();
			this.SetDebugInfo(stringBuilder.ToString() + " => sign:" + text3);
			return this.GetParameter("sign").ToLower().Equals(text3);
		}
	}
}
